package com.retirement.activity.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.retirement.activity.domain.OldActivity;
import com.retirement.activity.dto.OldActivityAddDto;
import com.retirement.activity.dto.OldActivityDto;
import com.retirement.activity.mapper.OldActivityMapper;
import com.retirement.activity.service.IOldActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retirement.activity.vo.OldActivityVo;
import com.retirement.audit.domain.OldAudit;
import com.retirement.audit.mapper.OldAuditMapper;
import com.retirement.audit.service.impl.OldAuditServiceImpl;
import com.retirement.common.DataTypeEnum;
import com.retirement.common.utils.SecurityUtils;
import com.retirement.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijy
 * @since 2023-12-22
 */
@Service
public class OldActivityServiceImpl extends ServiceImpl<OldActivityMapper, OldActivity> implements IOldActivityService {

    @Autowired
    private OldAuditServiceImpl oldAuditService;

    @Resource
    private OldAuditMapper oldAuditMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addActivity(OldActivity dto) {
        String id = IdUtil.simpleUUID();
        dto.setId(id).setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now())
                .setApplicantId(SecurityUtils.getUserId().toString())
                .setName(SecurityUtils.getUsername().toString())
                .setState("0");
        if(ObjectUtil.isNotEmpty(dto.getApplicantId())){
            dto.setState("2");
            oldAuditService.addAudit(id, DataTypeEnum.OLD_ACTIVITY.getTable(),dto);
        }

        return baseMapper.insert(dto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivity(OldActivityAddDto dto) {
        OldActivity bean = baseMapper.selectById(dto.getId());
        Assert.isTrue(!bean.getState().equals("2"), "审核中的数据不可修改");
        BeanUtils.copyProperties(dto,bean);
        bean.setUpdateTime(LocalDateTime.now());
        if(ObjectUtil.isNotEmpty(dto.getApplicantId())){
            OldAudit oldAudit = oldAuditMapper.selectOne(new LambdaQueryWrapper<OldAudit>()
                    .eq(OldAudit::getDataId,dto.getId()).
                    eq(OldAudit::getDataType,DataTypeEnum.OLD_ACTIVITY.getTable())
                    .last("limit 1").orderByDesc(OldAudit::getSortingNo));
            Integer sortingNo = oldAudit.getSortingNo() + 1;
            dto.setSortingNo(sortingNo);
            bean.setState("2");
            oldAuditService.addAudit(bean.getId(), DataTypeEnum.OLD_ACTIVITY.getTable(),dto);
        }

        return baseMapper.updateById(bean);
    }

    @Override
    public List<OldActivity> listOldActivity(OldActivityDto dto) {
        dto.setApplicantId(SecurityUtils.getUserId().toString());
        return baseMapper.listOldActivity(dto);
    }

    @Override
    public OldActivityVo getDetail(String id) {
        OldActivity bean = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(bean), "该活动信息已被删除");
        OldActivityVo vo = new OldActivityVo();
        BeanUtils.copyProperties(bean,vo);
        if(bean.getState() != null && bean.getState() != "0"){
            List<OldAudit> oldAuditList = oldAuditService.list(new LambdaQueryWrapper<OldAudit>()
                    .eq(OldAudit::getDataId,id)
                    .eq(OldAudit::getDataType,DataTypeEnum.OLD_ACTIVITY.getTable()));
            if(ObjectUtil.isNotNull(oldAuditList)){
                vo.setOldAuditList(oldAuditList);
            }
        }
        return vo;
    }

    @Override
    public int deleteOldActivity(String id) {
        OldActivity bean = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotNull(bean), "活动信息不存在");
        Assert.isTrue(!bean.getState().equals("2"), "审核中的数据不可删除");

        return baseMapper.deleteById(id);
    }

}
